<?php
class Admin_Model_TinNhanMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Admin_Model_DbTable_TinNhan');
		}
		return $this->_dbTable;
	}

	public function getDb(){
		if ($this->db===null) {
			$this->db=Zend_Registry::get('connectDB');
		}
		return $this->db;
	}

	/*
	 * save function
	 */
	public function save(Admin_Model_TableColumn_TinNhan $item, $task = null) {
		$now=date("Y-m-d H:i:s",time());
		try {
			$data = $item->toArray();
			$data['ngay_gui']=$now;
			if ($item->getId()>0) {
				$id = $item->getId();
				$this->getDbTable()->update($data, $this->getDbTable()->getAdapter()->quoteInto('id=?', $id));
			}
			else{
				$this->getDbTable()->insert($data);
			}
				
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}

	/*
	 * delete function
	 */
	public function delete($id = 0, $key = 'id') {
		$where = $key . ' IN (' . $id . ')';
		$this->getDbTable()->delete($where);
	}

}